package com.example.mos4_1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.Date;

//横向滑动查看各个进度 纵向滚动查看项目(纵向交给外部scrollview)
public class ProjectProgressView extends View {

    private int horizontalSpace  = 90;

    //分割线画笔
    private Paint dividerPaint;
    //灰色进度条画笔
    private Paint undoneProgressPaint;
    //蓝色进度条画笔
    private Paint doneProgressPaint;
    //当天竖线画笔
    private Paint curDayPaint;
    //项目数量
    private int projectCount;
    //时间跨度 单位 月
    private int monthCount = 12;
    //月份画笔
    private Paint textPaint;
    //所有项目最早开始月份
    private Date indexDate;
    //所有项目最晚结束月份
    private Date endDate;
    //起始月份 int
    private int indexMonth;
    //结束月份 int
    private int endMonth;
    private float monthWidth=30*3*1.0f;
    //40dp后面转换单位
    private int height = 120+146;
    //测试用的初始数据
    private float startPos = 1.5f;
    //指的是截止日期位置
    private float finishPos = 10.6f;
    private int xoffset = 69;
    private int yoffset = 100;
//    private float actuallyStartPos = 1.5f;
    //项目完成位置的进度 要求不超过finishpos
    private float donePos = 3.5f;
    private float curDayPos = 5.5f;
    private int undoneProgressColor;
    private int doneProgressColor;
    private int dividerColor;
    //进度条的宽度
    private int progressHeight = 15*3;
    //当日时间戳线的颜色
    private int curDayColor = 0xFF76A2FF;
    public ProjectProgressView(Context context) {
        super(context);
        init();
    }

    public ProjectProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProjectProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProjectProgressView);
        dividerColor = typedArray.getColor(R.styleable.ProjectProgressView_DividerColor,0xFFEAEAEA);
        undoneProgressColor = typedArray.getColor(R.styleable.ProjectProgressView_UPColor,0xFFEAEAEA);
        doneProgressColor = typedArray.getColor(R.styleable.ProjectProgressView_DPColor,0xFFEAEAEA);
        monthWidth = typedArray.getDimension(R.styleable.ProjectProgressView_MonthWidth,120f);
        typedArray.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       setMeasuredDimension((int) (monthCount*monthWidth)+xoffset,height);
    }

    private void init(){
        dividerPaint = new Paint();
        dividerPaint.setColor(Color.parseColor("#FFEAEAEA"));
        dividerPaint.setStrokeWidth(3);
        dividerPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        dividerPaint.setStyle(Paint.Style.STROKE);
        curDayPaint = new Paint();
        curDayPaint.setColor(curDayColor);
        curDayPaint.setStrokeWidth(9);
        curDayPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        curDayPaint.setStyle(Paint.Style.STROKE);
        undoneProgressPaint = new Paint();
        undoneProgressPaint.setColor(Color.parseColor("#FFDFDFDF"));
        undoneProgressPaint.setStrokeWidth(1);
        undoneProgressPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        undoneProgressPaint.setStyle(Paint.Style.FILL);
        doneProgressPaint = new Paint();
        doneProgressPaint.setColor(0xff4680f9);
        doneProgressPaint.setStrokeWidth(1);
        doneProgressPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        doneProgressPaint.setStyle(Paint.Style.FILL);
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint.setTextSize(14*3);
        textPaint.setColor(0xFF444444);
        textPaint.setTextAlign(Paint.Align.CENTER);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(xoffset,0);
        for (int i = 0; i < monthCount; i++) {
            //画竖线
            //位置要根据ui调整
            canvas.drawLine(horizontalSpace*i,146,horizontalSpace*i,getHeight(),dividerPaint);
            canvas.save();
        }
        drawProgress(canvas);
        drawCurDay(canvas);
        drawMonthText(canvas);
    }

    /**
     * 画所有进度条
     * @param canvas 画布
     */
    private void drawProgress(Canvas canvas){
        //画灰色进度条
        canvas.drawRect(startPos*monthWidth,(height+146-progressHeight)/2,finishPos*monthWidth,(height+146+progressHeight)/2,undoneProgressPaint);
        canvas.save();
        canvas.restore();
        //画已完成进度条
        if(donePos>0){
            canvas.drawRect(startPos*monthWidth,(height+146-progressHeight)/2,donePos*monthWidth,(height+146+progressHeight)/2,doneProgressPaint);
        }


    }

    private void drawCurDay(Canvas canvas){
        canvas.drawLine(curDayPos*monthWidth,146,curDayPos*monthWidth,getHeight(),curDayPaint);
    }

    /**
     * 设置项目进度数据 转换为实际进度和预期时间 并计算时间跨度 和表格起始点
     */
    public void setData(){

    }


    /**
     *  根据最早开始的项目和最晚结束的项目计算月份跨度 不小于12
     * @return monthCount 月数
     */
    private int getMonthCount(){
        boolean lessThanYear = true;
        if(lessThanYear){
            return 12;
        }else {
            return 0;
        }

    }

    /**
     * 将日期转换为数据
     * @param date 日期
     */
    private void dateConvert(Date date){

    }

    /**
     * 考虑移除此方法 交给外部判断
     * 传入最早的开始时间和最晚的结束时间 计算view的长度 注意 比如2019年1月到2020年1月 因为最后1月要画一格
     * 所以 实际要画13格 影响此view的宽度计算
     * @param indexDate 最早的开始时间
     * @param endDate 最晚的结束时间
     */
    public void setProgressMaxWidth(Date indexDate,Date endDate){
        //最晚时间的月减去最早时间的月 加上年份差（12进制 所以乘以12）
        monthCount = (endDate.getMonth()-indexDate.getMonth())+(endDate.getYear()-indexDate.getYear())*12+1;
        indexMonth = indexDate.getMonth()+1;
        endMonth = indexMonth+monthCount;
        this.indexDate = indexDate;
        this.endDate = endDate;
        requestLayout();
    }

    /**
     * 设置进度条起始和结束位置 精确到天 天按照 day除以当月最大天数的比例
     * @param startDate 此项目的开始日期
     * @param finishDate 此项目的结束日期,即截止时间
     * @param today 今天
     * @param doneDate 实际上完成的进度最新时间
     */
    public void setDateArea(Date startDate,Date finishDate,
    Date today,Date doneDate
    ){
        //改为list集合数据 需要遍历
//        int indexDateDay = startDate.getDay();
        int startMonthDays = DateTools.getMonthDays(startDate.getYear(), startDate.getMonth() + 1);
//        int endDateDay = finishDate.getDay();
        int finishMonthDays = DateTools.getMonthDays(finishDate.getYear(), finishDate.getMonth() + 1);
        //计算起始和结束位置的坐标
        int startDay = startDate.getDate();
        startPos = (startDate.getMonth()-indexDate.getMonth())+(startDate.getYear()-indexDate.getYear())*12
                +startDay*1.0f/startMonthDays*1.0f;
        int finishDay = finishDate.getDate();
        finishPos = (finishDate.getMonth()-indexDate.getMonth())+(finishDate.getYear()-indexDate.getYear())*12
                +finishDay*1.0f/finishMonthDays*1.0f;
//        //今天
        int doneMonthDays = DateTools.getMonthDays(doneDate.getYear(), doneDate.getMonth() + 1);
        int doneDay = doneDate.getDate();
        donePos = (doneDate.getMonth()-indexDate.getMonth())+(doneDate.getYear()-indexDate.getYear())*12
                +doneDay*1.0f/doneMonthDays*1.0f;
        //进度最新日期当月的最大天数
        int todayMonthDays = DateTools.getMonthDays(today.getYear(), today.getMonth() + 1);
        int todayDaY = today.getDate();
        donePos = (today.getMonth()-indexDate.getMonth())+(today.getYear()-indexDate.getYear())*12
                +todayDaY*1.0f/todayMonthDays*1.0f;
//        if(actuallyStartPos>startPos){
//            actuallyStartPos = startPos;
//        }
        //最新进度不可能超过当天
        if(startPos<0){
            startPos = 0;
        }
        if(curDayPos<donePos){
            curDayPos = donePos;
        }
        //最新进度不可能超过截止时间
        if(donePos>finishPos){
            donePos = finishPos;
        }
        invalidate();
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_DOWN:
                break;
                default:
                    break;
        }
        return super.onTouchEvent(event);
    }

    /**
     *
     * @param canvas
     */
    private void drawMonthText(Canvas canvas){
        for (int i = 0; i < monthCount; i++) {
            String text;
            if(indexMonth+i<=12){
                text = indexMonth+i+"";
            }else {
                text = (indexMonth+i)%12+"";
            }
            canvas.drawText(text,horizontalSpace*i,140,textPaint);
            if(indexMonth+i>12&&(indexMonth+i)%12==1){
                canvas.drawText(indexDate.getYear()+(indexMonth+i)/12+"",horizontalSpace*i
                ,48,textPaint);
            }
        }
    }
}
